﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;
using MenusHunter.Services.Utilities;

namespace MenusHunter.Services
{
    /// <summary>
    /// Summary description for RestaurantsManager1
    /// </summary>
    [WebService(Namespace = "http://menuhunter.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class RestaurantsManager1 : WebService
    {
        [WebMethod]
        public List<Entities.Restaurant> GetListRestaurant()
        {
            using (var db = new MenusHunterDataContext())
            {
                return (from item in db.Restaurants
                        where item.Status == 1
                        select
                            new Entities.Restaurant()
                                {
                                    Id = item.Id,
                                    AccountId = (int) item.AccountId,
                                    Name = item.Name,
                                    Description = item.Description,
                                    Latitude = item.Latitude,
                                    Longtitude = item.Longtitude,
                                    Address = item.Address,
                                    Phone = item.Phone,
                                    Photo = item.Photo,
                                    IsFeature = (bool) item.IsFeature,
                                    DateCreate = (DateTime) item.DateCreate,
                                    Status = (int) item.Status
                                }).ToList();
            }
        }

        [WebMethod]
        public int NewRestaurant(int accountId, string name, string desciption, string photo, double latitude,
                                 double longtitude, string address, string phone, bool isFeature, int status)
        {
            using (var db = new MenusHunterDataContext())
            {
                var item = new Restaurant()
                               {
                                   AccountId = accountId,
                                   Name = name,
                                   Description = desciption,
                                   Phone = photo,
                                   Latitude = latitude,
                                   Longtitude = longtitude,
                                   Address = address,
                                   Photo = photo,
                                   DateCreate = DateTime.Now,
                                   IsFeature = isFeature,
                                   Status = status
                               };

                db.Restaurants.InsertOnSubmit(item);
                db.SubmitChanges();
                return item.Id;
            }
        }

        [WebMethod]
        public List<Entities.Restaurant> GetListRestaurantsAround(double currentLat, double currrentLong, double radius,
                                                                  int offset, int limit)
        {
            using (var db = new MenusHunterDataContext())
            {
                var restaurants = db.Restaurants.OrderBy(t => t.Name).ToList();
                var rowNum = 0;
                var totalRow = 0;
                var list = new List<Entities.Restaurant>();
                foreach (var restaurant in restaurants)
                {
                    if (rowNum++ >= offset)
                    {
                        if (totalRow++ >= limit) break;
                        double dicstance = Utils.CalcDistance2Point(currentLat, currrentLong, restaurant.Latitude,
                                                                    restaurant.Longtitude);
                        if (dicstance < radius)
                            list.Add(new Entities.Restaurant
                                         {
                                             Id = restaurant.Id,
                                             AccountId = (int) restaurant.AccountId,
                                             Name = restaurant.Name,
                                             Description = restaurant.Description,
                                             Phone = restaurant.Phone,
                                             Latitude = restaurant.Latitude,
                                             Longtitude = restaurant.Longtitude,
                                             Address = restaurant.Address,
                                             Photo = restaurant.Photo,
                                             DateCreate = (DateTime) restaurant.DateCreate,
                                             IsFeature = (bool) restaurant.IsFeature,
                                             Status = (int) restaurant.Status
                                         });
                    }
                }
                return
                    list;
            }
        }
    }
}